package com.wxtf.platform.main.contraceptives;


import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wxtf.platform.interfaces.CommonsCallProvider;
import com.wxtf.platform.plugins.user.UserBean;
import com.wxtf.platform.supers.LogicBase;
import com.wxtf.platform.tools.AccessDataBaseUtil;
import com.wxtf.platform.tools.ConstantInfoHelper;

public class TmaApplyDetailAction extends LogicBase implements
		CommonsCallProvider {

	private static Log log = LogFactory.getLog(TmaApplyDetailAction.class);

	public TmaApplyDetailAction(String jsonArray) {

	}
	
	/** 
	 * 描述：delete 删除记录
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @author sun_xiaoliang
	 */
	@Override
	public Object delete(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("ids");
		String[] ids = id.split(",");
		String sql = "delete from tma_apply_detail where tmaad_id = ? ";
		AccessDataBaseUtil.deleteByIds(sql, ids);
		return "\"删除成功\"";
	}

	@Override
	public Object insert(Object jsonArray) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object recordCount(Object jsonArray) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	/** 
	 * 描述：selectTmGeneralDictionary 查询药具类型
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @author sun_xiaoliang
	 */
	public Object selectTmGeneralDictionary(Object jsonArray) throws Exception {		
		sql = "select tmgd_id,tmgd_name from tm_general_dictionary where tmgd_is_valid='1' and tmgd_type='105' order by tmgd_order_num";
		String[] fields = {"tmgd_id","tmgd_name"};
		JSONArray array = JSONArray.fromObject(AccessDataBaseUtil.query(fields, sql, null));
		return array;
	}
	
	/** 
	 * 描述：selectTmaDictionary 查询药具名称
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @author sun_xiaoliang
	 */
 
	public Object selectTmaDictionary(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String tmgd_id = jsonObject.getString("tmgd_id");
		sql = "select tmad_id,tmad_name from tma_dictionary where tmad_status=0 and reserve1=1 and  tmad_kind=?";
		String[] fields = {"tmad_id","tmad_name"};
		JSONArray temp = JSONArray.fromObject(AccessDataBaseUtil.query(fields, sql, new Object[]{tmgd_id}));
		return temp;
	}
	
	/** 
	 * 描述：selectTmaDictionaryById 根据Id查询
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @author sun_xiaoliang
	 */
 
	public Object selectTmaDictionaryById(Object jsonArray) throws Exception {
		UserBean user = (UserBean)(this.getRequest()).getSession().getAttribute("userBean");
		String CurOrgId = user.getCurOrgId();
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String tmad_id = jsonObject.getString("tmad_id");
		sql = "select tmad_description,tmad_spec,tmad_unit,tmad_company_name,tmad_batch_code,tmad_qgp,tmad_price,(SELECT Sum(kucun.num)   FROM kucun WHERE  kucun.orgId =  '"+CurOrgId+"' AND  kucun.id =  tma_dictionary.tmad_id  ) as num from tma_dictionary where tmad_id=?";
		String[] fields = {"tmad_description","tmad_spec","tmad_unit","tmad_company_name","tmad_batch_code","tmad_qgp","tmad_price","tmad_num"};
		JSONArray temp = JSONArray.fromObject(AccessDataBaseUtil.query(fields, sql, new Object[]{tmad_id}));
		return temp.toString().substring(1, temp.toString().length()-1);
	}

	@Override
	public Object run() {
		Object msg = "";
		try {
			if ("queryPage".equals(this.getMethodName())) {// 列表
				msg = this.select(this.getLogicData());
				return msg.toString();
			} else if ("selectTmGeneralDictionary".equals(this.getMethodName())) {// 查询药具类型
				msg = this.selectTmGeneralDictionary(this.getLogicData());
				return msg.toString();
			} else if ("selectTmaDictionary".equals(this.getMethodName())) {// 查询药具名称
				msg = this.selectTmaDictionary(this.getLogicData());
				return msg.toString();
			} else if ("delete".equals(this.getMethodName())) {// 删除
				msg = this.delete(this.getLogicData());
			} else if ("selectTmaDictionaryById".equals(this.getMethodName())) {// 查询药具名称
				msg = this.selectTmaDictionaryById(this.getLogicData());
				return msg.toString();
			} else {
				msg = "\"没有【" + this.getMethodName() + "】该方法\"";
			}
			if(msg == null || "[null]".equals(msg)){
				msg = "\"\"";
			}
			msg = ConstantInfoHelper.generateJSONString(msg.toString()); 
		} catch (Exception e) {
			msg = "操作失败，请联系管理员";
			msg = ConstantInfoHelper.generateJSONString(ConstantInfoHelper.JSON_HEAD, ConstantInfoHelper.getFailResultDetail(msg.toString()), false);
			log.error(e, e);
		} finally {
			this.clear();
		}
		return msg.toString();
	}
	
 
	/** 
	 * 描述：select 查询记录
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @author sun_xiaoliang
	 */
	@Override
	public Object select(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String tmaam_id = jsonObject.getString("tmaam_id");
		sql = " from TMA_DICTIONARY t inner join tma_apply_detail d on t.tmad_id=d.tmad_id and d.tmaam_id=? left join tm_general_dictionary g on t.tmad_kind=g.tmgd_id";
		String countSql = "select count(*)" + sql;
		String querySql = "select d.tmaad_id,t.tmad_name,d.tmad_id as t_tmad_name,g.tmgd_name,g.tmgd_id as t_tmgd_name,t.tmad_company_name,t.tmad_price,d.tmaad_num,  ROUND(  t.tmad_price*d.tmaad_num ,2)  as  tmaad_num_all,tmad_qgp,t.tmad_batch_code,t.tmad_unit,t.tmad_spec,t.tmad_description " + sql;
		String[] fields = {"tmaad_id","tmad_name","t_tmad_name","tmgd_name","t_tmgd_name","tmad_company_name","tmad_price","tmaad_num","tmaad_num_all","tmad_qgp","tmad_batch_code","tmad_unit","tmad_spec","tmad_description"};
		return AccessDataBaseUtil.query4SQLPagging(fields, countSql, querySql, this.getPage(), this.getPagesize(), new Object[]{ tmaam_id });
	} 

	@Override
	public Object setRequest(HttpServletRequest request) throws Exception {	
		this.request = request;
		return request;
	}

	@Override
	public Object update(Object jsonArray) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object updateBatch(Object jsonArray) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object onCreate(Object jsonArray) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object onDestory(Object jsonArray) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object onInit(Object jsonArray) {
		// TODO Auto-generated method stub
		return null;
	}

}
